#include <wfcClient.h>
#include <wfcSession.h>
#include <wxyutils.h>
#include <wxyDrawing.h>
#include <wxySimprepCtrl.h>
#include <ProToolkitErrors.h>
#include "main.h"

wfcStatus ReverseSimp()
{
    pfcSession_ptr session = pfcGetProESession();
    pfcWindow_ptr window = session->GetCurrentWindow();
    try
    {
        pfcModel_ptr model = session->GetCurrentModel();
        pfcSolid_ptr solid = pfcSolid::cast(model);
        pfcSimpRep_ptr SimpRep = solid->GetActiveSimpRep();
        pfcSimpRep_ptr SRType = SimpRep->GetSimpRepType();
        pfcSimpRepInstructions_ptr SRInst = SimpRep->GetInstructions();
        if (SRInst)
        {
            pfcSimpRepActionType DefaultAction = SRInst->GetDefaultAction();
            pfcSimpRepItems_ptr SRItems = SRInst->GetItems();
            xint count = SRItems->getarraysize();
            pfcSimpRepItems_ptr NewSRItems = pfcSimpRepItems::create();
            if (count > 0)
            {
                for (xint i = 0; i < count; i++)
                {
                    pfcSimpRepItem_ptr SRItem = SRItems->get(i);
                    pfcSimpRepAction_ptr SRAction = SRItem->GetAction();
                    pfcSimpRepActionType SRAType = SRAction->GetType();
                    pfcSimpRepItem_ptr NewSRItem = pfcSimpRepItem::Create(SRItem->GetItemPath());
                    switch (SRAType)
                    {
                    case pfcSIMPREP_INCLUDE:
                        NewSRItem->SetAction((pfcSimpRepAction_ptr)pfcSimpRepExclude::Create());
                        break;
                    case pfcSIMPREP_EXCLUDE:
                        NewSRItem->SetAction((pfcSimpRepAction_ptr)pfcSimpRepInclude::Create());
                        break;
                    default:
                        NewSRItem->SetAction(NULL);
                        break;
                    }
                    NewSRItems->append(NewSRItem);
                }
            }
            pfcCreateNewSimpRepInstructions_ptr NewSRInst;
            try
            {
                pfcSimpRep_ptr TempRep = solid->GetSimpRep("TempRep");
                solid->DeleteSimpRep(TempRep);
            }
            OTK_EXCEPTION_PRINT_LOG
            NewSRInst = pfcCreateNewSimpRepInstructions::Create("TempRep");

            switch (DefaultAction)
            {
            case pfcSIMPREP_INCLUDE:
                NewSRInst->SetDefaultAction(pfcSIMPREP_EXCLUDE);
                break;
            case pfcSIMPREP_EXCLUDE:
                NewSRInst->SetDefaultAction(pfcSIMPREP_INCLUDE);
                break;
            default:
                NewSRInst->SetDefaultAction(pfcSIMPREP_EXCLUDE);
                break;
            }
            NewSRInst->SetItems(NewSRItems);
            NewSRInst->SetIsTemporary(xfalse);
            pfcSimpRep_ptr NewSR = solid->CreateSimpRep(NewSRInst);
            solid->ActivateSimpRep(NewSR);
            wfcWSolid_ptr wsolid = wfcWSolid::cast(solid);
            wsolid->DisplaySolid();
        }
    }

    OTK_EXCEPTION_PRINT_LOG
    return wfcTK_NO_ERROR;
}

wfcStatus ShowAllSimp()
{
    pfcSession_ptr session = pfcGetProESession();
    try
    {
        pfcModel_ptr model = session->GetCurrentModel();
        pfcSolid_ptr solid = pfcSolid::cast(model);
        pfcSimpRep_ptr MasterRep = solid->GetMasterRep();
        solid->ActivateSimpRep(MasterRep);
        wfcWSolid_ptr wsolid = wfcWSolid::cast(solid);
        wsolid->DisplaySolid();
    }
    OTK_EXCEPTION_PRINT_LOG
    return wfcTK_NO_ERROR;
}
wfcStatus HideSimp()
{
    pfcSession_ptr session = pfcGetProESession();
    try
    {
        pfcModel_ptr model = session->GetCurrentModel();
        pfcSolid_ptr solid = pfcSolid::cast(model);
        //获取当前激活的简化表示
        pfcSimpRep_ptr SimpRep = solid->GetActiveSimpRep();
        pfcSimpRep_ptr SRType = SimpRep->GetSimpRepType();
        pfcSimpRepInstructions_ptr SRInst = SimpRep->GetInstructions();
        pfcSimpRepItems_ptr NewSRItems = pfcSimpRepItems::create();
        pfcSimpRepActionType DefaultAction;
        //如果当前显示为主表示，GetInstructions()返回值为NULL
        if (SRInst)
        {
            //新建简化表示的默认显示与当前进化表示保持一致
            DefaultAction = SRInst->GetDefaultAction();
            pfcSimpRepItems_ptr SRItems = SRInst->GetItems();
            xint count = SRItems->getarraysize();
            if (count > 0)
            {
                //将当前简化表示中包含的设置项添加到新建的简化表示中，避免与反向显示交替使用时将反向显示后的设置项丢失
                NewSRItems->insertseq(0, SRItems);
            }
        }
        else
        {
            DefaultAction = pfcSIMPREP_INCLUDE;
        }
        pfcCreateNewSimpRepInstructions_ptr NewSRInst;
        try
        {
            //创建临时SimpRep前如果存在“TempRep”，先删除
            //删除前要先提取出当前简化表示的DefaultActions和SimpRepItems
            pfcSimpRep_ptr TempRep = solid->GetSimpRep("TempRep");
            solid->DeleteSimpRep(TempRep);
        }
        OTK_EXCEPTION_PRINT_LOG
        //创建临时SimpRep TempRep
        NewSRInst = pfcCreateNewSimpRepInstructions::Create("TempRep");
        //设为临时表示，视图列表中不显示，但实际存在
        NewSRInst->SetIsTemporary(xtrue);
        NewSRInst->SetDefaultAction(DefaultAction);
        pfcSelectionBuffer_ptr SelBuff = session->GetCurrentSelectionBuffer();
        pfcSelections_ptr sels = SelBuff->GetContents();
        //未选择模型时，sels为NULL
        if (sels)
        {
            xint SelCount = sels->getarraysize();
            for (int i = 0; i < SelCount; i++)
            {
                //不管是选择part，还是选择了part中的元素，ComponentPath都是part在assembly中的path
                pfcComponentPath_ptr ComPath = sels->get(i)->GetPath();
                xintsequence_ptr ComPathIds = ComPath->GetComponentIds();
                // SimpRep中使用的CompItemPath即为ComponentPath中的Ids
                pfcSimpRepCompItemPath_ptr SRIPath = pfcSimpRepCompItemPath::Create(ComPathIds);
                pfcSimpRepItem_ptr SRItem = pfcSimpRepItem::Create((pfcSimpRepItemPath_ptr)SRIPath);
                SRItem->SetAction((pfcSimpRepAction_ptr)pfcSimpRepExclude::Create());
                NewSRItems->append(SRItem);
            }
            NewSRInst->SetItems(NewSRItems);
            pfcSimpRep_ptr NewSimpRep = solid->CreateSimpRep(NewSRInst);
            solid->ActivateSimpRep(NewSimpRep);
            wfcWSolid_ptr wsolid = wfcWSolid::cast(solid);
            wsolid->DisplaySolid(); //该函数会报异常XToolkitFound，不写该函数新建简化表示后不显示
            // LOG(LOG_ERROR);
            // pfcWindow_ptr window = session->GetCurrentWindow();
            // wfcWWindow_ptr wwindow = wfcWWindow::cast(window);
            // wwindow->Refit();
            // window->Refresh();
        }
    }
    OTK_EXCEPTION_PRINT_LOG
    return wfcTK_NO_ERROR;
}